//
//  BYJRegisterViewController.m
//  BLDMusics
//
//  Created by 白云杰 on 16/3/28.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "BYJRegisterViewController.h"

@interface BYJRegisterViewController ()<UITextFieldDelegate>

@property (nonatomic, retain) UITextField *numberTextField;
@property (nonatomic, retain) UITextField *testTextField;
@property (nonatomic, retain) UITextField *nameTextField;

@end

@implementation BYJRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createControls];
}

- (void)createControls {
    
    self.numberTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 30)];
    _numberTextField.delegate = self;
    _numberTextField.borderStyle = UITextBorderStyleRoundedRect;
    _numberTextField.clearButtonMode = UITextFieldViewModeAlways;
    _numberTextField.returnKeyType=UIReturnKeyGo;
    _numberTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    _numberTextField.placeholder = @"输入手机号";
    [self.view addSubview:_numberTextField];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, SCREEN_WIDTH, 30)];
    label.backgroundColor = [UIColor clearColor];
    label.text = @"注册即同意《白云杰用户协议》";
    [self.view addSubview:label];
    
    UIButton *getButton = [UIButton buttonWithType:UIButtonTypeCustom];
    getButton.layer.cornerRadius = 5;
    getButton.layer.masksToBounds = YES;
    [getButton setTitle:@"下一步" forState:UIControlStateNormal];
    getButton.frame = CGRectMake(SCREEN_WIDTH / 2 - SCREEN_WIDTH / 6, 80, SCREEN_WIDTH / 3, 30);
    getButton.backgroundColor = [UIColor cyanColor];
    [getButton addTarget:self action:@selector(getButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:getButton];
    
    self.testTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 120, SCREEN_WIDTH, 30)];
    _testTextField.delegate = self;
    _testTextField.borderStyle = UITextBorderStyleRoundedRect;
    _testTextField.clearButtonMode = UITextFieldViewModeAlways;
    _testTextField.returnKeyType=UIReturnKeyGo;
    _testTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    _testTextField.placeholder = @"输入验证码";
    [self.view addSubview:_testTextField];
    
    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
    testButton.layer.cornerRadius = 5;
    testButton.layer.masksToBounds = YES;
    [testButton setTitle:@"下一步" forState:UIControlStateNormal];
    testButton.frame = CGRectMake(SCREEN_WIDTH / 2 - SCREEN_WIDTH / 6, 160, SCREEN_WIDTH / 3, 30);
    testButton.backgroundColor = [UIColor cyanColor];
    [testButton addTarget:self action:@selector(testButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:testButton];

    self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 200, SCREEN_WIDTH, 30)];
    _nameTextField.delegate = self;
    _nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    _nameTextField.clearButtonMode = UITextFieldViewModeAlways;
    _nameTextField.returnKeyType=UIReturnKeyGo;
    _nameTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    _nameTextField.placeholder = @"输入昵称";
    [self.view addSubview:_nameTextField];

    UIButton *goButton = [UIButton buttonWithType:UIButtonTypeCustom];
    goButton.layer.cornerRadius = 5;
    goButton.layer.masksToBounds = YES;
    [goButton setTitle:@"确定" forState:UIControlStateNormal];
    goButton.frame = CGRectMake(SCREEN_WIDTH / 2 - SCREEN_WIDTH / 6, 240, SCREEN_WIDTH / 3, 30);
    goButton.backgroundColor = [UIColor cyanColor];
    [goButton addTarget:self action:@selector(goButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:goButton];
}

- (void)getButtonAction:(UIButton *)button {
    
    [BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:_numberTextField.text andTemplate:_testTextField.text resultBlock:^(int number, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        } else {
            //获得smsID
            NSLog(@"sms ID：%d",number);
        }
    }];

}

- (void)testButtonAction:(UIButton *)button {
    
    [BmobSMS verifySMSCodeInBackgroundWithPhoneNumber:_numberTextField.text andSMSCode:_testTextField.text resultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            NSLog(@"%@", @"验证成功，可执行用户请求的操作");
        } else {
            NSLog(@"%@", error);
        }
    }];
}

- (void)goButtonAction:(UIButton *)button {
    
    BmobUser *bUser = [[BmobUser alloc] init];
    [bUser setUsername:_nameTextField.text];
    [bUser setPassword:_testTextField.text];
    [bUser signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            NSLog(@"Sign up successfully");
        } else {
            NSLog(@"%@", error);
        }
    }];
    
    
    RootViewController *VC = [[RootViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}

// 获取短信验证码
- (void)ddd {
    [BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:@"" andTemplate:@"test" resultBlock:^(int number, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        } else {
            //获得smsID
            NSLog(@"sms ID：%d",number);
        }
    }];
}

// 验证短信验证码
- (void)aaaa {
    
    [BmobSMS verifySMSCodeInBackgroundWithPhoneNumber:@"" andSMSCode:@"" resultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            NSLog(@"%@", @"验证成功，可执行用户请求的操作");
        } else {
            NSLog(@"%@", error);
        }
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    // 取消第一响应者
    [textField resignFirstResponder];
    // 回收键盘
    [textField endEditing:YES];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
